<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/25 0025
 * Time: 上午 11:15
 *
 *不用模块测试
 *
 */

namespace app\Login\controller;

use app\Login\model\AdminUser as AdminUser;
use think\Db;

class Login
{

    // 设置完整的数据表（包含前缀）
    protected $table = 'vote_admin_user';

    // 增加单个管理员
    public function addUser()
    {
        $user = new AdminUser;
        $user->user_name = "zhang";
        $user->password = md5("123456");
        $user->email = "abc123@163.com";
        if ($user->save()) {
            return "新增管理员".$user->user_name."成功！";
        } else {
            $user->getError();
        }

    }

    // 批量增加管理员
    public function addList()
    {
        $user = new AdminUser();
        $list = [
            ['user_name'=>'张三','password'=>md5('123456'),'email'=>'123654@qq.com'],
            ['user_name'=>'廖二','password'=>md5('123456'),'email'=>'654321@qq.com']
        ];
        if ($user->saveAll($list)) {
            return '批量增加管理员成功！';
        } else {
            return $user->getError();
        }

    }

    // 查询单条数据
    public function read($uid='')
    {
//        $user = new AdminUser();
//        $data = $user->find(10);
//        dump($data);

//        $user = AdminUser::get($uid);
//        echo $user['user_name']."<br/>";

//        $user = AdminUser::getByEmail('312943879@qq.com');
//        echo $user['user_name'];

//        $user = AdminUser::get(['user_name'=>'廖二']);
//        echo $user['email'];

        // 闭包配合查询构建器查询
        $user = AdminUser::get(function($query){
        $query->where('user_name','廖二')->where('uid','>',1)->order('uid','desc');
    });
        echo $user['email']."<br/>";
    }

    // 查询多条数据
    public function showList()
    {
        $list = AdminUser::all(function($query){
            $query->where('uid','>',5);
        });
        foreach($list as $user) {
            echo $user['user_name']."<br/>";
            echo $user['email']."<br/>";
            echo "<hr/>";
        }
    }

    // 更新数据
    public function updateUser($uid='')
    {
        $user['uid'] = intval($uid);
        $user['user_name'] = 'liaopengjie';
        $user['email'] = 'liaopengjie@qq.com';

        if (AdminUser::update($user)) {
            return '更新管理员信息成功！';
        } else {
            return '更新管理员信息失败！';
        }

    }

    // 删除数据
    public function deleteUser($uid)
    {
//        $user = AdminUser::get($uid);
//        if($user) {
//            $user->delete();
//            return '删除管理员成功！';
//        } else {
//            return '删除的管理员不存在！';
//        }

        $user = AdminUser::destroy($uid);
        if ($user) {
            return '删除管理员成功！';
        } else {
            return '删除的管理员不存在！';
        }

    }



    public function login()
    {
        echo "不同模块下的登录测试！";
    }

    public function index()
    {
        echo "不同模块下的默认方法！";
    }





}